package com.dataseek.iot.device.manager.handler;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.core.handler.ServiceHandler;
import com.dataseek.iot.core.response.ResponseEntity;
import com.dataseek.iot.mysql.entity.Device;
import com.dataseek.iot.mysql.entity.DeviceRecord;
import com.dataseek.iot.mysql.entity.Product;
import com.dataseek.iot.mysql.service.impl.DeviceRecordServiceImpl;
import com.dataseek.iot.mysql.service.impl.DeviceServiceImpl;
import com.dataseek.iot.mysql.service.impl.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cc
 * @Title: 设备记录handler
 * @Description: 设备记录handler
 * @date 2021/11/119:36 上午
 * @since 1.0
 */
@Service
public class DeviceRecordHandler extends ServiceHandler<DeviceRecord> {

    @Autowired
    DeviceRecordServiceImpl deviceRecordService;

    @Autowired
    DeviceServiceImpl deviceService;

    @Autowired
    ProductServiceImpl productService;

    @Override
    public Mono<ResponseEntity> saveOne(Object entity) {
        Boolean isSaved =  deviceRecordService.save((DeviceRecord) entity);
        return this.createResponse(isSaved);
    }

    @Override
    public Mono<ResponseEntity> saveBatch(List lists) {
        Boolean isSaved =  deviceRecordService.saveBatch(lists);
        return this.createResponse(isSaved);
    }

    @Override
    public Mono<ResponseEntity> removeById(String id) {
        Boolean isRemoved =  deviceRecordService.removeById(id);
        return this.createResponse(isRemoved);
    }

    @Override
    public Mono<ResponseEntity> removeAll(List ids) {
        Boolean isRemoved = deviceRecordService.removeByIds(ids);
        return this.createResponse(isRemoved);
    }

    @Override
    public Mono<ResponseEntity> remove(Wrapper wrapper) {
        Boolean isRemoved = deviceRecordService.remove(wrapper);
        return this.createResponse(isRemoved);
    }

    @Override
    public Mono<ResponseEntity> remove(String id) {
        Boolean isRemoved = deviceRecordService.removeById(id);
        return this.createResponse(isRemoved);
    }


    @Override
    public Mono<ResponseEntity> updateOne(Object entity) {
        Boolean isUpdated = deviceRecordService.updateById((DeviceRecord) entity);
        return this.createResponse(isUpdated);
    }

    @Override
    public Mono<ResponseEntity> update(Wrapper wrapper) {
        Boolean isUpdated = deviceRecordService.update(wrapper);
        return this.createResponse(isUpdated);
    }

    @Override
    public Mono<ResponseEntity> updateBatch(List entities) {
        Boolean isUpdated = deviceRecordService.updateBatchById(entities);
        return this.createResponse(isUpdated);
    }

    @Override
    public Mono<ResponseEntity> findOne(String id) {
        DeviceRecord p = deviceRecordService.getById(id);
        return this.createResponse(p);
    }

    @Override
    public Mono<ResponseEntity> page(Page p) {
        Page page = deviceRecordService.page(p);
        return this.createResponse(page);
    }

    @Override
    public Mono<ResponseEntity> page(Page p, Wrapper wrapper) {
        Page page = deviceRecordService.page(p, wrapper);
        List<DeviceRecord> records = page.getRecords();
        List<DeviceRecord> result  = new ArrayList<>();
        for(DeviceRecord record : records){
            String deviceCode = record.getDeviceCode();
            Device d = deviceService.getDeviceByCode(deviceCode);
            String productCode = record.getProductCode();
            Product product = productService.getProductByCode(productCode);
            record.setDeviceName(d.getDeviceName());
            record.setProductName(product.getProductName());
            result.add(record);
        }
        page.setRecords(result);
        return this.createResponse(page);
    }

    @Override
    public Flux<ResponseEntity> find(List ids) {
        List<DeviceRecord>  dictProducts = deviceRecordService.listByIds(ids);
        return this.createResponse(dictProducts);
    }

    @Override
    public Flux<ResponseEntity> find(Wrapper wrapper) {
        List list = deviceRecordService.list(wrapper);
        return this.createResponse(list);
    }

    @Override
    public Flux<ResponseEntity> findAll() {
        List list = deviceRecordService.list();
        return this.createResponse(list);
    }

    @Override
    public Mono<ResponseEntity> defaultResponseOne(ResponseEntity responseEntity) {
        return Mono.just(responseEntity);
    }

    @Override
    public Flux<ResponseEntity> defaultResponseTwo(ResponseEntity responseEntity) {
        return Flux.just(responseEntity);
    }
}
